﻿/**
 * GoText Palm: A program that allows to send text messages over the internet.
 *  Copyright (C) 2008 Nicola 'Gawaine' Racco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Data.SqlTypes;
using System.Collections.Generic;
using System.Text;

namespace GoTextPalm.Core.Data
{
    public class Service : IComparable<Service>
    {
        public class ServiceConfig
        {
            public ServiceConfig() { }

            public SqlString Username;
            public SqlString Password;
            public SqlString Nickname;
        }

        public class ServiceStat
        {
            public ServiceStat() { }

            public DateTime Date;
            public Int32 Sent;
        }

        public enum MaxMessagesMode { DAILY = 0, MONTHLY = 1 }
        public enum ServiceType { EMAIL = 0, PHONE = 1, BOTH = 2 }
        internal enum ServiceNeeds { USER = 1, PASSWORD = 2, NICK = 4 }

        internal ServiceNeeds Needs;
        public SqlInt32 Id;
        public String Name;
        public String Url;
        public SqlString Welcome;
        public Int32 MaxRecipiens;
        public Int32 MaxChars;
        public ServiceType Type;
        public Int32 MaxMessages;
        public MaxMessagesMode MMMode;
        public Dictionary<Char, String> CharsToReplace = new Dictionary<char,string>();
        public Dictionary<Char, Int32> CharsToRecount = new Dictionary<char,int>();
        public ServiceConfig Config;
        public ServiceStat Stat;
        public List<Message> Messages = new List<Message>();
        public LastMessage LastMessage;

        public Boolean NeedsUsername
        {
            get
            {
                return (Needs & ServiceNeeds.USER) == ServiceNeeds.USER;
            }
            set
            {
                if (value)
                    Needs |= ServiceNeeds.USER;
                else if (NeedsUsername)
                    Needs &= ~ServiceNeeds.USER;
            }
        }

        public Boolean NeedsPassword
        {
            get
            {
                return (Needs & ServiceNeeds.PASSWORD) == ServiceNeeds.PASSWORD;
            }
            set
            {
                if (value)
                    Needs |= ServiceNeeds.PASSWORD;
                else if (NeedsPassword)
                    Needs &= ~ServiceNeeds.PASSWORD;
            }
        }

        public Boolean NeedsNickname
        {
            get
            {
                return (Needs & ServiceNeeds.NICK) == ServiceNeeds.NICK;
            }
            set
            {
                if (value)
                    Needs |= ServiceNeeds.NICK;
                else if (NeedsNickname)
                    Needs &= ~ServiceNeeds.NICK;
            }
        }

        public int CompareTo(Service s)
        {
            return Name.CompareTo(s.Name);
        }
    }
}
